package com.thermofisher.dsc.amanda.common.util;

import com.thermofisher.dsc.amanda.security.config.KeyConfiguration;
import com.thermofisher.dsc.amanda.security.jwt.IJWTInfo;
import java.security.interfaces.RSAPublicKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by wenjie.yang on 9/28/2018.
 */
@Component
@Slf4j
public class JwtUtil {
    @Value("${jwt.expire:18000}")
    private int expire;

    @Autowired
    private KeyConfiguration keyConfiguration;


    public boolean isValidFromThirdParty(String message, String signature){
        RSAPublicKey rsaPublicKey = keyConfiguration.getGrowthClickRSAKey();
        return RSAWithThirdParty.verify(rsaPublicKey, message, signature);
    }
    /**
     * generate token
     * @param jwtInfo
     * @return
     * @throws Exception
     */
    public String generateToken(IJWTInfo jwtInfo) throws Exception {
        return JWTHelper.generateToken(jwtInfo, keyConfiguration.getUserPriKey(),expire);
    }

    /**
     * parse token
     * @param token
     * @return
     * @throws Exception
     */
    public IJWTInfo getInfoFromToken(String token) throws Exception {
        return JWTHelper.getInfoFromToken(token, keyConfiguration.getUserPubKey());
    }

}
